﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web;
using System.Reflection;

namespace SFast.Web
{
    public static class FormExtensions
    {
        #region Request扩展函数 _GetForm  对象反射方式从表单中获取对象属性所相应值
        /// <summary>
        /// 对象反射方式从表单中获取对象属性所相应值
        /// </summary>
        /// <typeparam name="T">反射对象</typeparam>
        /// <param name="request">HttpRequest 对象 </param>
        /// <returns></returns>
        public static T _GetForm<T>(this HttpRequest request)
            where T : class,new()
        {
            Type t = typeof(T);   //对象反射
            T nt = new T();  //创建一个新的对象
            //遍历对象的所有属性 给对象的属性复制
            Array.ForEach<PropertyInfo>(t.GetProperties(), p =>
            {
                if (!string.IsNullOrEmpty(request[p.Name]))
                {
                    string oriValue = request[p.Name];
                    object objValue = null;
                    //给值封装，确保给对象的属性复制时，数据类型相对应
                    if (p.PropertyType.IsGenericType)  //判断是否为泛型类型
                    {
                        if (p.PropertyType.GetGenericTypeDefinition().FullName == "System.Nullable`1")
                        {
                            if (p.PropertyType.GetGenericArguments()[0].Name == "Boolean")
                            {
                                if (oriValue == "0" || oriValue.ToLower() == "false")
                                {
                                    oriValue = "false";
                                }
                                else
                                {
                                    oriValue = "true";
                                }
                            }
                            objValue = Convert.ChangeType(oriValue, p.PropertyType.GetGenericArguments()[0]);
                        }
                        else
                        {
                            throw new Exception("不识别的泛型类型。");
                        }
                    }
                    else
                    {
                        if (p.PropertyType.Name == "Boolean")
                        {
                            if (oriValue == "0" || oriValue.ToLower() == "false")
                            {
                                oriValue = "false";
                            }
                            else
                            {
                                oriValue = "true";
                            }
                        }
                        objValue = Convert.ChangeType(oriValue, p.PropertyType);
                    }
                    p.SetValue(nt, objValue, null);  //给对象类型赋值
                }
            });
            return nt;
        }

        #endregion

        #region _DataToForm   把对象添加到表单中
        /// <summary>
        /// 把对象添加到表单中。
        /// </summary>
        /// <param name="formid">表单ID</param>
        /// <param name="data">数据对象</param>
        /// <param name="map">如果数据对象属性名和表单元素name值不一样时，可以加入映射对应，{对象属性 = 表单元素名}</param>
        /// <returns></returns>
        public static void _DataToForm(this object obj, String formid, object map = null)
        {
            MyJs.ScriptManager((HttpContext.Current.CurrentHandler as Page), "<script type=\"text/javascript\">myform({ formid: '" + formid + "', data: " + obj ._ToJsonStr()+ "})</script>", 1, "key");
        }
        #endregion
    }
}
